﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml.Linq;

namespace 美妆产品管理系统.Customers
{
    public partial class Update_customer : Form
    {
        public Update_customer()
        {
            InitializeComponent();
        }

        SqlDataAdapter dacustomer,dalog;
        DataSet ds = new DataSet();
        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtID.Text = dgvcustomer.CurrentRow.Cells["Customer_ID"].Value.ToString();
            txtname.Text = dgvcustomer.CurrentRow.Cells["Customer_Name"].Value.ToString();
            txtemail.Text = dgvcustomer.CurrentRow.Cells["Email"].Value.ToString();
            txtphone.Text = dgvcustomer.CurrentRow.Cells["phome"].Value.ToString();
        }

        void init()
        {
            DB.GetCn();
            string str = "select * from Customers";
            dacustomer = new SqlDataAdapter(str, DB.cn);
            string sdr = "select * from Tlog";
            dalog = new SqlDataAdapter(sdr, DB.cn);
            dacustomer.Fill(ds, "customer_info");
            dalog.Fill(ds, "log_info");
            DB.cn.Close();
        }

        void showAll()
        {
            DataView dvcustomer = new DataView(ds.Tables["customer_info"]);
            dgvcustomer.DataSource = dvcustomer;
        }
       void dgvHead()
       {
            this.dgvcustomer.Columns[0].HeaderText = "客户编号";
            this.dgvcustomer.Columns[1].HeaderText = "客户名称";
            this.dgvcustomer.Columns[2].HeaderText = "邮箱";
            this.dgvcustomer.Columns[3].HeaderText = "密码";
       }

        private void Update_customer_Load(object sender, EventArgs e)
        {
            init();
            showAll();
            dgvHead();
        }

        private void btnno_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            if (txtID.Text == "" || txtname.Text == "")
            {
                MessageBox.Show("客户编号或客户名称不能为空");
            }
            else
            {
                
                DialogResult dr = MessageBox.Show("您确定要进行修改吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    int a = dgvcustomer.CurrentRow.Index;
                    string srt = dgvcustomer.Rows[a].Cells["Customer_ID"].Value.ToString();
                    DataRow[] CustRows = ds.Tables["customer_info"].Select("Customer_ID='" + srt + "'");
                    CustRows[0]["Customer_Name"] = txtname.Text.Trim();
                    CustRows[0]["Customer_ID"] = txtID.Text.Trim();
                    CustRows[0]["Email"] = txtemail.Text.Trim();
                    CustRows[0]["phome"] = txtphone.Text.Trim();
                    MessageBox.Show("修改成功");

                    DataRow drlog = ds.Tables["log_info"].NewRow();
                    drlog["username"] = Login.username;
                    drlog["type"] = "修改";
                    drlog["action_date"] = DateTime.Now;
                    drlog["action_table"] = "Customers表";
                    ds.Tables["log_info"].Rows.Add(drlog);

                    UpdateDB();
                }
                else
                {
                    return;
                }
            
            }
        }

        void UpdateDB()
        {
            SqlTransaction st = tansactionInit();
            try
            {
                dacustomer.Update(ds, "customer_info");
                dalog.Update(ds, "log_info");
                st.Commit();
                DB.cn.Close();
            }
            catch(SqlException ex)
            {
                st.Rollback();
                DB.cn.Close();
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private SqlTransaction tansactionInit()
        {
            DB.GetCn();
            SqlCommandBuilder dbcustomers = new SqlCommandBuilder(dacustomer);
            SqlCommandBuilder dblog = new SqlCommandBuilder(dalog);

            dacustomer.UpdateCommand = dbcustomers.GetUpdateCommand();
            dalog.UpdateCommand = dblog.GetUpdateCommand();

            SqlTransaction st = DB.cn.BeginTransaction();
            dacustomer.UpdateCommand.Transaction = st;
            dalog.UpdateCommand.Transaction = st;

            dacustomer.UpdateCommand.Connection = DB.cn;
            dalog.UpdateCommand.Connection = DB.cn;

            return st;
        }
    }
}
